using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Runtime.Serialization;
using EnvDTE;
using Microsoft.Practices.Common;
using Microsoft.Practices.RecipeFramework.Library;
using System.Diagnostics;
using System.Security.Permissions;
using Microsoft.Practices.RecipeFramework.Extensions.References.VisualStudio;

namespace Microsoft.Practices.RepositoryFactory.References
{
    [Serializable]
    public class ProjectItemPropertyReferenceVB : ProjectPropertyReferenceVB
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ProjectItemPropertyReference"/> class.
        /// </summary>
        /// <param name="recipe">The recipe.</param>
        public ProjectItemPropertyReferenceVB(string recipe)
            : base(recipe)
        {
        }

        /// <summary>
        /// Required constructor for deserialization.
        /// </summary>
        protected ProjectItemPropertyReferenceVB(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Performs the validation of the item passed as target
        /// Returns true if the reference is allowed to be executed in the target
        /// that is if the target is a project that has the mapping property.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public override bool OnIsEnabledFor(object target)
        {
            ProjectItem item = target as ProjectItem;

            if (item != null)
            {
                FileCodeModel fcm = null;

                try
                {
                    fcm = item.FileCodeModel;
                }
                catch
                {
                    // do nothing. This throws an error with aspx pages.
                }


                if (fcm != null)
                {
                    return base.OnIsEnabledFor(item.ContainingProject);
                }
            }

            return false;
        }

        #endregion
    }
}

